package javase.ch05.homeWork.Demo3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.CountDownLatch;

public class ReadThread implements Runnable{
    Socket socket;
    private CountDownLatch countDownLatch;
    public ReadThread(Socket socket, CountDownLatch countDownLatch){
        this.socket = socket;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try(
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                ){
            String message;
            while((message=br.readLine())!=null){
                if(message.equals("886")){
                    System.out.println(socket.getRemoteSocketAddress() +"已下线886");
                    break;
                }else{
                    System.out.println(socket.getRemoteSocketAddress() +"说：" + message);
                }

            }
        }catch (SocketException  e){
            System.out.println(socket.getRemoteSocketAddress() +"已下线");
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            countDownLatch.countDown();
        }
    }
}
